﻿namespace EFS.RC.CCAdmin.Surveys.Survey
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32? surveyID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.Survey_GetCompleted += UtilitiesService_Survey_GetCompleted;

			//set new survey response listener
			UtilitiesService.Survey_CreateCompleted += UtilitiesService_Survey_CreateCompleted;

			//set edit response listener
			UtilitiesService.Survey_UpdateCompleted += UtilitiesService_Survey_UpdateCompleted;

			//load templates
			UtilitiesService.Survey_GetAllCompleted += new System.EventHandler<WebProxy.Survey_GetAllCompletedEventArgs>(UtilitiesService_Survey_GetAllCompleted);

			//load template surveys

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.SurveyID = surveyID;
		}

		#endregion

		#region Properties
		/// Property SurveyID: Get, Set
		public System.Int32? SurveyID { get; set;}

		/// Property IsValid: Get
		public System.Boolean? IsValid
		{
			get
			{
				return EFS.Common.Verify.String(SurveyName) && SurveyType.HasValue && SurveyReviewType.HasValue;
			}
		}

		/// Property IsValid: Get
		public System.String SurveyName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxSurveyName.Text);
			}
		}

		/// Property SurveyType: Get
		public EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType? SurveyType
		{
			get
			{
				EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType> typeHelper =
					new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType>(comboboxType);
				return typeHelper.SelectedValue;
			}
			set
			{
				EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType> typeHelper =
					new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType>(comboboxType);
				typeHelper.SelectedValue = value;
			}
		}

		/// Property SurveyReviewType: Get
		public EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? SurveyReviewType
		{
			get
			{
				EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType> reviewTypeHelper =
					new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>(comboboxReviewType);
				return reviewTypeHelper.SelectedValue;
			}
			set
			{
				EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType> reviewTypeHelper =
					new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>(comboboxReviewType);
				reviewTypeHelper.SelectedValue = value;
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// populate
			EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType> reviewTypeHelper =
				new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>(comboboxReviewType);
			reviewTypeHelper.Populate();
			SurveyReviewType = EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType.Agent;

			// populate
			EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType> typeHelper =
				new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType>(comboboxType);
			typeHelper.Populate();
			SurveyType = EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType.Conversation;

			// if edit
			if (SurveyID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

				// call ws
				UtilitiesService.Survey_GetAsync(SurveyID.Value);

				surveyCategoriesList.Visibility = System.Windows.Visibility.Visible;

				dockPanelCreateFromTemplate.Visibility = System.Windows.Visibility.Collapsed;
				checkBoxTemplate.Visibility = System.Windows.Visibility.Collapsed;

				return;
			}

			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

			// call ws
			UtilitiesService.Survey_GetAllAsync(1, null, null);

			surveyCategoriesList.Visibility = System.Windows.Visibility.Collapsed;
		}
		#endregion

		#region WS Callbacks
		/// GetSurveyDetailsCompleted
		private void UtilitiesService_Survey_GetCompleted(object sender, WebProxy.Survey_GetCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(e.Result.ToString());

				//bind/fill form
				textBoxSurveyName.Text = survey.Name;

				// set review type
				EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType> reviewTypeHelper =
					new EFS.Common.UI.Controls.ComboBoxEnumHelper<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>(comboboxReviewType);
				reviewTypeHelper.SelectedValue = survey.ReviewType;

				// since we loaded, notify children
				surveyCategoriesList.SetSurveyId(this.SurveyID);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve monitoring. " + ex.Message);
			}
		}

		void UtilitiesService_Survey_GetAllCompleted(object sender, WebProxy.Survey_GetAllCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				//check if request was successful
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveys =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[]>(e.Result.ToString());

				// bind/fill form
				cbTemplates.ItemsSource = surveys;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data. " + ex.Message);
			}
		}

		/// Create survey completed
		private void UtilitiesService_Survey_CreateCompleted(object sender, WebProxy.Survey_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				this.SurveyID = e.Result;
				//EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "The new monitoring has been created, you may now add categories!", null);
				System.Windows.MessageBox.Show("The new monitoring has been created, you may now add categories!");
				LoadData();
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create monitoring! " + ex.Message);
			}
		}

		/// Update survey completed
		private void UtilitiesService_Survey_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update monitoring. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (!EFS.Common.Functions.Nullable<System.Boolean>.GetSafe(IsValid, false))
			{
				return;
			}

			// create
			if (!SurveyID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while creating monitoring");

				EFS.RC.CC.CH.Entities.Surveys.SurveyInfo survey = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo)cbTemplates.SelectedItem;

				//ws call
#warning FIX THIS
				UtilitiesService.Survey_CreateAsync(
					textBoxSurveyName.Text,
					(System.Int32)EFS.Common.Functions.Nullable<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType>.GetSafe(SurveyType, "SurveyType"),
					((checkBoxTemplate.IsChecked.HasValue && (bool)checkBoxTemplate.IsChecked) ? 1 : 0), 
					((survey != null) ? survey.ID.Value : 0),
					(System.Int32)EFS.Common.Functions.Nullable<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>.GetSafe(SurveyReviewType, "SurveyReviewType")
				);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while updating monitoring");

				//ws call
#warning FIX THIS
				UtilitiesService.Survey_UpdateAsync(
					SurveyID.Value,
					(System.Int32)EFS.Common.Functions.Nullable<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType>.GetSafe(SurveyType, "SurveyType"),
					textBoxSurveyName.Text,
					(System.Int32)EFS.Common.Functions.Nullable<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType>.GetSafe(SurveyReviewType, "SurveyReviewType"));
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}

}